package com.qdlc.p2p.biz.service.user.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.user.UserPayPasswordService;
import com.qdlc.p2p.biz.util.ValidCodeSender;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.common.util.code.MD5;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.User;

@Service
public class UserPayPasswordServiceImpl implements UserPayPasswordService {

	@Resource
	UserDao userDao;
	
	/**
	 * [交易密码]检测参数
	 * @param user 操作用户
	 * @param payPassword 原始交易密码
	 * @param newPayPassword 新交易密码
	 * @param confirmNewPayPassword 确认新交易密码
	 * @return
	 */
	private ReturnResult checkPayPassword(User user, String payPassword, String newPayPassword, String confirmNewPayPassword) {
		
		if (StringUtil.isBlank(newPayPassword)) {
			return new ReturnResult("请输入新交易密码!");
		}
		
		if (StringUtil.isBlank(confirmNewPayPassword)) {
			return new ReturnResult("请输入确认新密码!");
		}
		
		//用户交易密码,如果用户交易密码为空,表示首次设置,不用校验原始密码
		String userPayPassword = user.getPayPwd();
		if (StringUtil.isNotBlank(userPayPassword)) {
			if (StringUtil.isBlank(payPassword)) {
				return new ReturnResult("请输入原交易密码!");
			}
			String newPayPasswordMD5 = MD5.encode(payPassword);
			if(!newPayPasswordMD5.equals(userPayPassword)){
				return new ReturnResult("原交易密码错误!");
			}
		}
		
		//校验密码长度及组成
		String checkPwd = ValidateUtil.isPwd(newPayPassword);
		if (StringUtil.isNotBlank(checkPwd)) {
			return new ReturnResult(checkPwd);
		}

		if (newPayPassword.equals(payPassword)) {
			return new ReturnResult("新交易密码不能和原交易密码相同!");
		}
		
		if (!confirmNewPayPassword.equals(newPayPassword)) {
			return new ReturnResult("新交易密码和确认交易密码不相同!");
		}

		return ReturnResult.SUCCESS;
	}
	
	/**
	 * [交易密码]修改交易密码
	 * @param user 操作用户
	 * @param payPassword 原始交易密码
	 * @param newPayPassword 新交易密码
	 * @param confirmNewPayPassword 确认交易密码
	 * @return
	 */
	@Override
	public ReturnResult modifyPayPassword(User user, String payPassword, String newPayPassword, String confirmNewPayPassword){
		//检测交易密码
		ReturnResult result = checkPayPassword(user, payPassword, newPayPassword, confirmNewPayPassword);
		if(result.isError()){
			return result;
		}
		
		//修改交易密码
		String newPayPasswordMD5 = MD5.encode(newPayPassword);
		user.setPayPwd(newPayPasswordMD5);
		userDao.modifyPaypwd(user);
		
		return ReturnResult.SUCCESS;
	}
	
	/**
	 * [交易密码]检测重置密码参数
	 * @param user
	 * @param code
	 * @param newPayPassword
	 * @param confirmNewPayPassword
	 * @return
	 */
	private ReturnResult checkValidCodeForPayPassword(User user, String code, String newPayPassword, String confirmNewPayPassword){
		
		if (StringUtil.isBlank(code)) {
			new ReturnResult("请填写验证码!");
		}
		
		if (StringUtil.isBlank(newPayPassword)) {
			new ReturnResult("请输入新交易密码!");
		}
		
		if (StringUtil.isBlank(confirmNewPayPassword)) {
			new ReturnResult("请输入确认新密码!");
		}
		
		if (!newPayPassword.equals(confirmNewPayPassword)) {
			new ReturnResult("两次交易密码不一致!");
		}

		// 校验密码长度及组成
		String checkPwd = ValidateUtil.isPwd(newPayPassword);
		if (StringUtil.isNotBlank(checkPwd)) {
			new ReturnResult(checkPwd);
		}
		
		//检测验证码
		ReturnResult result = ValidCodeSender.checkValidCode(user, code, NoticeTplNid.NOTICE_GET_PAYPWD);
		
		return result;
	}
	
	/**
	 * [交易密码]重置交易密码
	 * @param user
	 * @param code
	 * @param newPayPassword
	 * @param confirmNewPayPassword
	 * @return
	 */
	@Override
	public ReturnResult modifyResetPayPassword(User user, String code, String newPayPassword, String confirmNewPayPassword){
		//检测验证码
		ReturnResult result = checkValidCodeForPayPassword(user, code, newPayPassword, confirmNewPayPassword);
		if(result.isError()){
			return result;
		}
		
		//重置交易密码
		String newPayPasswordMD5 = MD5.encode(newPayPassword);
		user.setPayPwd(newPayPasswordMD5);
		userDao.modifyPaypwd(user);
		
		return ReturnResult.SUCCESS;
	}
	
}
